package com.objectdraw.client.objectdraw;
import java.awt.*;
import java.awt.event.*;

/**
 * controls mouse and keyboard events for drawing text
 * @author jgulik, cfruehan
 *
 */
public class TextTool implements KeyboardTool {

	protected DrawingCanvas canvas;
	protected Point startingPosition;
	protected StringBuffer text;
	protected Font font = new Font("Serif", Font.BOLD, 24);
//	protected FontMetrics fm =
//	Toolkit.getDefaultToolkit().getFontMetrics(font); // deprecated method
//	protected int ascent = fm.getAscent();  //not needed here
//	protected int descent = fm.getDescent();  //not needed here

	/**
	 * instantiates the tool
	 * @param canvas the canvas upon which the tool acts
	 */
	public TextTool(DrawingCanvas canvas) {
		this.canvas = canvas;
	}
	TextObject mytext;
	
	/**
	 * start a new text object at the point supplied in e
	 */
	public void mousePressed(MouseEvent e)  {
		/* Establish Focus and starting position for text display */
		canvas.requestFocus();
		startingPosition = e.getPoint();
		Graphics iBGraphics = canvas.getImageBufferGraphics();
		iBGraphics.setFont(font);
		text = new StringBuffer();
		mytext = new TextObject(startingPosition,canvas.penColor, font);
		IDrawingObjectLink mytextLink=new IDrawingObjectLink(mytext);
		canvas.push(mytextLink);
	}

	/**
	 * add the character in event e to the end of the string then repaint
	 */
	public void keyPressed(KeyEvent e)  {
		/* add character to string buffer */
		char nextChar = e.getKeyChar(); 
		text.append(nextChar);
		mytext.setText(text.toString());
		//Graphics iBGraphics = canvas.getImageBufferGraphics();
		//iBGraphics.drawString(text.toString(), startingPosition.x,startingPosition.y); 
		canvas.repaint();
	}


	/**
	 * not implemented
	 */
	public void mouseReleased(MouseEvent e) { } 

	/**
	 * not implemented
	 */
	public void mouseDragged(MouseEvent e) { }
	//public void keyReleased(KeyEvent e) { }
	//public void keyTyped(KeyEvent e) { }
}
